<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>部门员工信息修改</title>
    <script src="/js/jquery-3.4.1.min.js"></script>
</head>
<body>
<h3>部门员工信息修改</h3>
<form id="frmedit">
    <input id="id" name="id" type="hidden"/>
    姓 名:<input id="empName" name="empName" type="text"/><br/>
    性 别:<input id="genderMan" name="gender" type="radio" value="1" checked/>男 &nbsp;
    <input id="genderWomen" name="gender" type="radio" value="0"/>女<br/>
    生 日:<input id="birthday" name="birthday" type="date"/><br/>
    入职日期:<input id="hireDate" name="hireDate" type="date"/><br/>
    工 资:<input id="salary" name="salary" type="text"/><br/>
    地 址:<input id="address" name="address" type="text"/><br/>
    部 门:<select name="deptId" id="selDeptId">
    <option value="0">请选择</option>
</select><br/>
    <input name="btnadd" id="btnadd" type="button" value="保存" onclick="doEditEmp()"/>
    <input name="btnreset" type="reset" value="重置"/>
</form>
</body>
<script>
    $(document).ready(function () {
        loadDepts();
    });

    function getEmpById() {
        var id = location.href.substring(location.href.indexOf("=") + 1);
        console.log("id:" + id);
        $.ajax({
            url: "/admin/empl/toedit/" + id,
            type: "get",
            dataType: "json",
            success: function (result) {
                if (result.errorCode == 200) {
                    var emp = result.data;
                    $("#empName").val(emp.empName);
                    if (emp.gender=="1"){
                        $("#genderMan").prop("checked",true)
                    }else {
                        $("#genderWomen").prop("checked",true)
                    }
                    $("#birthday").val(emp.birthday);
                    $("#hireDate").val(emp.hireDate);
                    $("#salary").val(emp.salary);
                    $("#address").val(emp.address);
                    $("#selDeptId").val(emp.deptId);
                    $("#id").val(emp.id);
                }
            }
        });
    }

    function doEditEmp() {
        $.ajax({
            url: "/admin/empl/doedit",
            type: "post",
            data: $("#frmedit").serialize(),
            dataType: "json",
            success: function (result) {
                if (result.errorCode == 200) {
                    window.location.href = "/pages/ajax.html";
                } else {
                    alert(result.errorMsg);
                }
            }
        });
    }

    function loadDepts() {
        $.ajax({
            url: "/admin/dept/list",
            type: "post",
            dataType: "json",
            success: function (result) {
                if (result.errorCode == 200) {
                    for (var i = 0; i < result.data.length; i++) {
                        var dept = result.data[i];
                        $("#selDeptId").append(`<option value="${dept.id}">${dept.deptName}</option>`);
                    }
                    getEmpById();
                }
            }
        });
    }
</script>
</html>